/**
 * 
 */
package kr.co.insoft.core.repository.template.application.impl;

import java.util.List;
import java.util.Map;

import kr.co.insoft.core.mapper.template.application.EventMapper;
import kr.co.insoft.core.model.template.application.EventEntityModel;
import kr.co.insoft.core.repository.template.application.EventRepository;
import kr.co.insoft.framework.exceptions.ExceptionCode;
import kr.co.insoft.framework.exceptions.RepositoryException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

/**
 * @author IN-SOFT Inc. Park Jong Taek(jtpark@in-soft.co.kr)
 * 
 */
@Repository
public class EventRepositoryImpl implements EventRepository {
	
	@Autowired
	EventMapper eventMapper;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * kr.co.insoft.core.repository.template.ScriptRepository#getLists(kr.
	 * co.insoft.core.model.paging.PagingModel)
	 */
	@Override
	public List<EventEntityModel> getList(Map<String, Object> param)
			throws RepositoryException {
		try {
			//Assert.notNull(pagingModel);
			//Assert.notNull(pagingModel.getCurrentPage());
		} catch (IllegalArgumentException e) {
			throw new RepositoryException(ExceptionCode.ARGUMENT_ERROR);
		}

		try {
			return eventMapper.getList(param);
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * kr.co.insoft.core.repository.template.ScriptRepository#getScript(
	 * kr.co.insoft.core.model.template.EventEntityModel)
	 */

	@Override
	public EventEntityModel getEvent(
			EventEntityModel eventEntityModel) throws RepositoryException {
		try {
			Assert.notNull(eventEntityModel);
			Assert.notNull(eventEntityModel.getEventid());
		} catch (IllegalArgumentException e) {
			throw new RepositoryException(ExceptionCode.ARGUMENT_ERROR);
		}

		try {
			return eventMapper.getEvent(eventEntityModel);
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * kr.co.insoft.core.repository.template.script.ScriptRepository#createScript
	 * (kr.co.insoft.core.model.template.script.EventEntityModel)
	 */
	@Override
	public EventEntityModel createEvent(
			EventEntityModel eventEntityModel) throws RepositoryException {
		try {
			Assert.notNull(eventEntityModel);
			Assert.notNull(eventEntityModel.getEventName());
			//Assert.hasText(scriptEntityModel.getScriptName());
			Assert.notNull(eventEntityModel.getUserid());
		} catch (IllegalArgumentException e) {
			throw new RepositoryException(ExceptionCode.ARGUMENT_ERROR);
		}
		try {
			eventEntityModel.setEventid(eventMapper.getUuid());
			eventMapper.createEvent(eventEntityModel);
			return eventEntityModel;
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * kr.co.insoft.core.repository.template.script.ScriptRepository#removeScript
	 * (kr.co.insoft.core.model.template.script.EventEntityModel)
	 */
	@Override
	public void doRemoveEvent(Map<String, Object> param)
			throws RepositoryException {
		try {
		//	Assert.notNull(eventEntityModel);
		//	Assert.notNull(eventEntityModel.getEventid());
		} catch (IllegalArgumentException e) {
			throw new RepositoryException(ExceptionCode.ARGUMENT_ERROR);
		}
		try {
		   eventMapper.doRemoveEvent(param);
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}
	
	
	/* (non-Javadoc)
	 * @see kr.co.insoft.core.repository.setting.cloud.CloudRepository#isExistsCloudName(kr.co.insoft.core.model.setting.cloud.CloudEntityModel)
	 */
	@Override
	public int isExistEventName(EventEntityModel eventEntityModel)
			throws RepositoryException {		
		try {
			Assert.notNull(eventEntityModel);
			Assert.notNull(eventEntityModel.getEventName());	
		} catch (IllegalArgumentException e) {
			throw new RepositoryException(ExceptionCode.ARGUMENT_ERROR);
		}
		
		try {
			return eventMapper.isExistEventName(eventEntityModel);
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}
	

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * kr.co.insoft.core.repository.template.script.ScriptRepository#doEditScript
	 * (kr.co.insoft.core.model.template.script.EventEntityModel)
	 */
	@Override
	public void doEditEvent(EventEntityModel eventEntityModel)
			throws RepositoryException {
		try {
			Assert.notNull(eventEntityModel);
			Assert.notNull(eventEntityModel.getEventid());
		} catch (IllegalArgumentException e) {
			throw new RepositoryException(ExceptionCode.ARGUMENT_ERROR);
		}
		try {
			eventMapper.doEditEvent(eventEntityModel);
		} catch (Exception e) {
			throw new RepositoryException(ExceptionCode.REPOSITORY_EXCEPTION, e);
		}
	}
}
